<template>
    <div>
        <home-header :nav="nav" :logo="logoInfo"></home-header>
        <public-top :title="detailsInfo.title"></public-top>
        <main class="main-box">
            <section class="content-box">
                <div class="content-left">
                    <div class="articl-box">
                        <div class="articl-wrop">
                            <div class="title">{{ detailsInfo.title }}</div>
                            <div class="reading-all">
                                <div class="read">阅读量:{{ detailsInfo.reading }}</div>
                                <div class="time">{{ detailsInfo.addtime }}</div>
                                <div class="type">
                                    <a :href="'../articles/' + detailsInfo.typeID">{{ detailsInfo.type }}</a>
                                </div>
                            </div>
                            <dzs-markdown :isPreview="true" v-if="detailsInfo.text_show_type == 'markdown' && detailsContent" class="markdown-wrop" :value="detailsContent"></dzs-markdown>
                            <div v-else class="html-wrop" v-html="decodeURIComponent(detailsInfo.content)"></div>
                        </div>
                        <div class="read-more">
                            <a :href="'../articles/' + detailsInfo.typeID">阅读更多内容</a>
                        </div>
                    </div>
                    <div class="comment-box">
                        <div class="title">评论:</div>
                        <comment-reply @submitSuccess="getComment"></comment-reply>
                        <div class="comment-item-public-box" v-for="item in commentList" :key="item.ID">
                            <comment-item :comment="item" @submitSuccess="getComment"
                                @deleteComment="getComment"></comment-item>
                            <div class="comment-item-list" v-if="item.list.length">
                                <template v-for="itm in item.list">
                                    <comment-item :comment="itm" @deleteComment="getComment"></comment-item>
                                </template>
                            </div>
                        </div>
                        <el-empty description="还没有人评论 快来占位置吧" v-if="!commentList.length"></el-empty>
                    </div>
                </div>
                <div class="content-right" :style="rightStyle">
                    <user-info :userInfo="userInfo"></user-info>
                    <my-label :labels="labels"></my-label>
                    <wbrs :wbrs="wbrs"></wbrs>
                </div>
            </section>
        </main>
        <public-footer :userInfo="logoInfo" :links="links"></public-footer>
        <scoll-top></scoll-top>
    </div>
</template>

<script>
import HomeHeader from '~/components/home-header/index.vue';
import publicFooter from "~/components/public-footer/index.vue";
import userInfo from "~/components/user-info/index.vue";
import myLabel from "~/components/my-label/index.vue";
import wbrs from "~/components/wbrs/index.vue";
import scollTop from "~/components/scoll-top/index.vue";
import publicTop from "~/components/public-top/index.vue";
import commentItem from "~/components/comment-item/index.vue";
import commentReply from "~/components/comment-reply/index.vue";
import request from '~/request/request';
import baudupush from '../../utils/baidupush';
import mixins from '~/mixins/mixins';
import dzsMarkdown from '~/components/dzs-markdown/index'
export default {
    name: 'Detals',
    async asyncData({ params, app }) {
        try {
            let nav = await request('/api/index/getNav');
            let logoInfo = await request('/api/index/getBlogTitle');
            let links = await request('/api/index/getLink');
            let userInfo = await request('/api/index/getMyUserInfo');
            let labels = await request('/api/index/getMyLable');
            let wbrs = await request('/api/index/getHotSearch');
            let details = await request('/api/article/getArticleById?ID=' + params.id);
            app.head.title = details.data.title;
            app.head.meta.push(
                {
                    name: 'keywords',
                    content: details.data.keywords
                },
                {
                    name: 'description',
                    content: details.data.brfie + details.data.title
                },
            )
            if (logoInfo.data.more_meta && JSON.parse(logoInfo.data.more_meta)) {
                app.head.meta = app.head.meta.concat(JSON.parse(logoInfo.data.more_meta));
            }
            return {
                nav: nav.data,
                logoInfo: logoInfo.data,
                links: links.data,
                userInfo: userInfo.data,
                labels: labels.data,
                wbrs: wbrs.data,
                detailsInfo: details.data
            }
        } catch (error) {
            return {}
        }
    },
    components: {
        HomeHeader,
        publicFooter,
        userInfo,
        myLabel,
        wbrs,
        scollTop,
        publicTop,
        commentItem,
        commentReply,
        dzsMarkdown
    },
    mixins: [mixins],
    data() {
        return {
            nav: [],
            logoInfo: {},
            links: [],
            userInfo: {},
            labels: [],
            wbrs: [],
            detailsInfo: {},
            commentList: [],
            detailsContent: "",
        }
    },
    mounted() {
        let params = this.$route.params;
        request('/api/article/getArticleById?ID=' + params.id).then(res => {
            this.detailsContent = decodeURIComponent(res.data.content);
        })
        baudupush();
        this.getComment();
    },
    methods: {
        /**获取评论*/
        getComment() {
            request('/api/comment/getCommentByArticleId?articleId=' + this.detailsInfo.ID).then(res => {
                this.commentList = res.data;
            })
        }
    },
}
</script>

<style lang="scss" scoped>
.main-box {
    width: 100%;
    background-color: #F8F9FB;
    position: relative;
    z-index: 2;

    .content-box {
        width: 100%;
        max-width: 1240px;
        margin: 0 auto;
        display: flex;
        justify-content: space-between;
        position: relative;
        padding: 20px;

        .content-left {
            width: 79%;

            .articl-box {
                width: 100%;
                background-color: #fff;
                border: #fff;
                padding: 16px;
                box-shadow: 0 4px 8px 6px rgba(7, 17, 21, 0.06);
                border-radius: 8px;
                cursor: pointer;
                overflow-y: auto;

                // 隐藏滚动条
                &::-webkit-scrollbar {
                    display: none;
                }
            }

            .comment-box {
                width: 100%;
                background-color: #fff;
                border: #fff;
                padding: 16px;
                box-shadow: 0 4px 8px 6px rgba(7, 17, 21, 0.06);
                border-radius: 8px;
                cursor: pointer;
                overflow-y: auto;
                margin-top: 20px;

                // 隐藏滚动条
                &::-webkit-scrollbar {
                    display: none;
                }

                .comment-item-public-box {
                    background-color: #fff;
                    border: #fff;
                    padding: 16px;
                    box-shadow: 0 4px 8px 6px rgba(7, 17, 21, 0.06);
                    border-radius: 8px;
                    margin-top: 10px;

                    .comment-item-list {
                        margin-left: 35px;
                    }
                }
            }

            .title {
                font-weight: 600;
                font-size: 20px;
                line-height: 46px;
            }

            .html-wrop {
                padding-bottom: 20px;
                word-wrap: break-word;
                overflow-x: auto;
            }

            .reading-all {
                display: flex;
                color: #999;
                font-size: 12px;
                align-items: center;
                padding: 20px 0;

                .read {
                    padding-right: 10px;
                }

                .time {
                    padding-right: 10px;
                }

                .type {
                    background-color: #e0e0e0;
                    padding: 2px 5px;
                    font-size: 14px;
                    border-radius: 3px;
                    transition: all 0.3s;

                    &:hover {
                        background-color: #aaa;
                        color: #fff;
                    }
                }
            }

            .read-more {
                text-align: center;
                font-size: 14px;

                a {
                    display: inline-block;
                    padding: 5px 10px;
                    border-radius: 5px;
                    cursor: pointer;
                    color: skyblue;

                    &:hover {
                        background-color: #aaa;
                        color: #fff;
                    }
                }
            }
        }

        .content-right {
            width: 20%;
        }

        .content-right-fixed {
            position: fixed;
            top: -170px;
        }

    }

    @media screen and (max-width: 768px) {
        .content-box {
            display: block;
            box-sizing: border-box;
            padding: 0;

            .content-left {
                width: 100%;
            }

            .content-right {
                display: none;
            }
        }
    }
}
</style>